package com.novellotus.iov.carvice.fragment;


import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.activity.SearchCarActivity;
import com.novellotus.iov.carvice.adapter.HomePageAdapter;
import com.novellotus.iov.carvice.adapter.HomePageEnterpriseAdapter;
import com.novellotus.iov.carvice.bean.RxEinfos;
import com.novellotus.iov.carvice.utils.HandlerUtils;
import com.novellotus.iov.carvice.utils.MyUtils;
import com.novellotus.iov.carvice.utils.MyViewUtils;
import com.novellotus.iov.carvice.utils.ResourcesManager;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomePagerFragment extends BaseFragment implements HandlerUtils.OnReceiveMessageListener {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    HomePageAdapter homePageAdapter;
    private HandlerUtils.HandlerHolder handlerHolder;

    public HomePagerFragment() {
        // Required empty public constructor
    }


    @Override
    View initRootView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home_page, container, false);
    }

    @Override
    void init() {
        handlerHolder = new HandlerUtils.HandlerHolder(this);
    }

    AlertDialog alertDialog;

    @OnClick(R.id.img_kefu)
    void call() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        alertDialog = builder.create();
        builder.setMessage("新荷平台客服电话:" + getResources().getString(R.string.phone));
        builder.setPositiveButton("拨打", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + getResources().getString(R.string.phone)));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                alertDialog.dismiss();
            }
        });

        builder.show();

    }

    @OnClick(R.id.img_search)
    void search() {
        Intent intent = new Intent(getActivity(), SearchCarActivity.class);
        intent.putExtra("type", 2);
        getContext().startActivity(intent);
        //  openActivity(getActivity(), SearchCarActivity.class);
    }


    public void setCompanyList(List<RxEinfos> list, HomePageEnterpriseAdapter.EnterpriseClickInterface enterpriseClickInterface) {
        try {
            homePageAdapter = new HomePageAdapter(getContext(), list, enterpriseClickInterface);
            recyclerView.setLayoutManager(MyViewUtils.getLinearLayoutManager(getActivity()));
            recyclerView.setAdapter(homePageAdapter);
        } catch (Exception e) {
            if (getContext() != null) {
                Message message = new Message();
                message.what = 0;
                message.obj = e.toString();
                handlerHolder.sendMessage(message);
            }
        }
    }


    @Override
    public void handlerMessage(Message msg) {
        MyUtils.showToast(getActivity(), "抱歉出現未知故障");
    }

}
